package com.ibm.spatiotemp.tools;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.fasterxml.jackson.databind.JsonNode;
import com.ibm.spatiotemp.server.constants.Constant;

public class DateParser {
	public static Logger logger = Logger.getLogger(DateParser.class);
	
	public static SimpleDateFormat date_formatter_ifx = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");/*format example 2014-01-01T00:00:20*/
	public static SimpleDateFormat date_formatter_other = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");/*format example 2014-01-01T00:00:20*/

	public static String FormatDate(long date) {
		String time = null;
		time = date_formatter_other.format(new Date(date));
		return time;
	}
	
	public static String FormatDate(long date, String dbclass) {
		String time = null;
		if(Constant.DB_CLASS_IFX.equals(dbclass))
		time = date_formatter_ifx.format(new Date(date));
		else
		time = date_formatter_other.format(new Date(date));
		return time;
	}
	
	//06/01/2015
	public static SimpleDateFormat[] DATE_FORMAT_DAY = new SimpleDateFormat[] {
			new SimpleDateFormat("MM/dd/yyyy"),
	};
	
	public static SimpleDateFormat[] DATE_FORMAT_MILLI = new SimpleDateFormat[] {
			new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"),
			new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS"),
			new SimpleDateFormat("yyyy.MM.dd HH:mm:ss-SSS"),
			new SimpleDateFormat("MM.dd.yyyy HH:mm:ss.SSS"),
			new SimpleDateFormat("yyyy-MM-dd HH:mm:ss-SSS"),
			new SimpleDateFormat("yyyy/MM/dd HH:mm:ss-SSS"),
			new SimpleDateFormat("MM/dd/yyyy HH:mm:ss.SSS"),
			new SimpleDateFormat("yyyy.MM.dd HH:mm:ss.SSS"),
			new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS"),
			};
	public static SimpleDateFormat[] DATE_FORMAT_SEC = new SimpleDateFormat[] {
			new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"),
			new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"),
			new SimpleDateFormat("MM/dd/yyyy HH:mm:ss"),
			new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"),
			new SimpleDateFormat("MM.dd.yyyy HH:mm:ss"),
			new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"),
			};
	public static SimpleDateFormat[] DATE_FORMAT_MIN = new SimpleDateFormat[] {
			new SimpleDateFormat("yyyy-MM-dd HH:mm"),
			new SimpleDateFormat("yyyy/MM/dd HH:mm"),
			new SimpleDateFormat("MM/dd/yyyy HH:mm"),
			new SimpleDateFormat("yyyy.MM.dd HH:mm"),
			new SimpleDateFormat("MM.dd.yyyy HH:mm"),
			new SimpleDateFormat("yyyy-MM-dd'T'HH:mm"),
			};

	/*for web side's date format to transform to webservice's query datestring format, now just for informix*/
	public static String transformDateFormat(String input_datestring)
	{
		long t = 0;
		try {
			t = DATE_FORMAT_DAY[0].parse(input_datestring).getTime();
		} catch (ParseException e) {
			try {
				t = DATE_FORMAT_SEC[5].parse(input_datestring).getTime();
			} catch (ParseException e1) {
				logger.error("cannot parse date:"+input_datestring);
			}
		}
		return FormatDate(t);
	}
	
	public static long getTimestamp(String date) throws ParseException {
		String dateString = date;
		int dateLength = dateString.length();
		SimpleDateFormat[] curDataFormats = null;
		if (dateString.indexOf('.', dateLength - 4) >= 0
				|| 
				(dateString.indexOf('-', dateLength - 4) >= 0
				&& StringUtils.countMatches(dateString, "T") == 0)) {
			curDataFormats = DATE_FORMAT_MILLI;
		} else {
			int count = StringUtils.countMatches(dateString, ":");
			if (count == 2)
				curDataFormats = DATE_FORMAT_SEC;
			else if (count == 1)
				curDataFormats = DATE_FORMAT_MIN;
		}
		if (curDataFormats != null) {
			for (int i = 0; i < curDataFormats.length; i++) {
				try {
					long t = curDataFormats[i].parse(dateString).getTime();
					//SimpleDateFormat tmp = curDataFormats[0];
					//curDataFormats[0] = curDataFormats[i];
					//curDataFormats[i] = tmp;
					return t;
				} catch (ParseException e) {
					continue;
				}
			}
		}
		try {
			return Long.parseLong(dateString);
		} catch (NumberFormatException e) {

		}
		throw new ParseException("Invalid date string: " + date, -1);
	}

	public final long getTimestamp(JsonNode date) throws ParseException {
		if (date.isTextual()) {
			String dateString = date.asText();
			int dateLength = dateString.length();
			SimpleDateFormat[] curDataFormats = null;
			if (dateString.indexOf('.', dateLength - 4) >= 0
					|| (dateString.indexOf('-', dateLength - 4) >= 0
							&& StringUtils.countMatches(dateString, "T") == 0)) {
				curDataFormats = DATE_FORMAT_MILLI;
			} else {
				int count = StringUtils.countMatches(dateString, ":");
				if (count == 2)
					curDataFormats = DATE_FORMAT_SEC;
				else if (count == 1)
					curDataFormats = DATE_FORMAT_MIN;
			}
			if (curDataFormats != null) {
				for (int i = 0; i < curDataFormats.length; i++) {
					try {
						long t = curDataFormats[i].parse(dateString).getTime();
						//SimpleDateFormat tmp = curDataFormats[0];
						//curDataFormats[0] = curDataFormats[i];
						//curDataFormats[i] = tmp;
						return t;
					} catch (ParseException e) {
						continue;
					}
				}
			}
			try {
				return Long.parseLong(dateString);
			} catch (NumberFormatException e) {

			}
		} else if (date.canConvertToLong()) {
			return date.asLong();
		}
		throw new ParseException("Invalid date string: " + date, -1);
	}
}
